package gui;


import shop.DateTransform;
import shop.Main;
import shop.ShoppingCart;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Vector;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;



/**
 * @author Wray
 */
public class ShoppingCartGui {
    JTable shoppingCartTable;
    JFrame shoppingCartGui = new JFrame("购物车");

    public ShoppingCartGui(ShoppingCart shoppingCart) {
        initShoppingCartGui(shoppingCart);
    }


    private void initShoppingCartGui(ShoppingCart shoppingCart) {


        shoppingCartGui.setLayout(null); // 清除布局函数
        shoppingCartGui.setResizable(false); // 设置窗体大小不可变

        shoppingCartGui.setLayout(new BorderLayout()); // 新建BorderLayout布局

        // 中间
        JPanel panelCenter = new JPanel(new FlowLayout());

        DefaultTableModel model = new DefaultTableModel() { // 表格数据不可改
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        Vector<Vector<Object>> date = new Vector<Vector<Object>>();
        Vector<String> names = new Vector<String>();
        names.add("商品名称");
        names.add("单价(元)");
        names.add("数量");
        DateTransform.changeList(Main.person.shoppingCart.getShoppingCartHashMap(), date);
        model.setDataVector(date, names);

        JTable shoppingCartTable = new JTable(model);// 创建一个表格，指定 所有行数据 和 表头
        SettingWindow.setTable(shoppingCartTable); // 设置表格

        JScrollPane commodityScrollPane = new JScrollPane(shoppingCartTable);

        panelCenter.add(commodityScrollPane);
        panelCenter.setBorder(new EmptyBorder(10, 10, 10, 10));
        shoppingCartGui.add(panelCenter, BorderLayout.CENTER);

        // 标题
        JPanel panelTitle = new JPanel(new GridLayout(2, 0));
        JLabel jl1 = new JLabel("我的购物车");
        jl1.setFont(new Font("微软雅黑", Font.BOLD, 25)); // 设置字体、样式、大小
        JLabel jl2 = new JLabel("共 " + shoppingCartTable.getRowCount() + " 件宝贝");
        jl2.setFont(new Font("微软雅黑", Font.PLAIN, 19)); // 设置字体、样式、大小
        panelTitle.add(jl1);
        panelTitle.add(jl2);
        panelTitle.setBorder(new EmptyBorder(10, 10, 10, 10));
        shoppingCartGui.add(panelTitle, BorderLayout.NORTH);


        // Display the window.
        shoppingCartGui.setSize(450, 750);
        shoppingCartGui.setVisible(false);
        // 设置窗口居中显示
        SettingWindow.setFrameNear(shoppingCartGui);}}
        //shoppingCartGui.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}}